<?php

namespace Admin\Model;

/**
 * ============================================================================
 * MXCMall开源商城
 * 官网地址:http://www.mothink.cn
 * 联系QQ:510858687
 * ============================================================================
 * 商品分类服务类
 */
class GoodsClassModel extends BaseModel {

    /**
     * 新增
     */
    public function insert() {
        $rd = array('status' => -1);
        $id = I("id", 0);
        $data = array();
        $data["className"] = I("className");
        if ($this->checkEmpty($data, true)) {
            $data["parentId"] = I("parentId", 0);
            $data["isShow"] = I("isShow", 0);
            $data["classSort"] = I("classSort", 0);
            $data["classFlag"] = 1;
            ;
            $m = M('goods_class');
            $rs = $m->add($data);
            if ($rs) {
                $rd['status'] = 1;
            }
        }
        return $rd;
    }

    /**
     * 修改
     */
    public function edit() {
        $rd = array('status' => -1);
        $id = (int) I("id", 0);
        $data = array();
        $data["className"] = I("className");
        if ($this->checkEmpty($data)) {
            $data["isShow"] = I("isShow", 0);
            $data["classSort"] = I("classSort", 0);
            $m = M('goods_class');
            $rs = $m->where("classFlag=1 and classId=" . I('id'))->save($data);
            if (false !== $rs) {
                if ($data['isShow'] == 0) {//修改子栏目是否隐藏
                    $this->editiIsShow();
                }
                $rd['status'] = 1;
            }
        }
        return $rd;
    }

    /**
     * 修改名称
     */
    public function editName() {
        $rd = array('status' => -1);
        $id = I("id", 0);
        $data = array();
        $data["className"] = I("className");
        if ($this->checkEmpty($data)) {
            $m = M('goods_class');
            $rs = $m->where("classFlag=1 and classId=" . I('id'))->save($data);
            if (false !== $rs) {
                $rd['status'] = 1;
            }
        }
        return $rd;
    }

    /**
     * 获取指定对象
     */
    public function get($id) {
        $m = M('goods_class');
        return $m->where("classId=" . $id)->find();
    }

    /**
     * 获取列表
     */
    public function queryByList($pid = 0) {
        $m = M('goods_class');
        $rs = $m->where('classFlag=1 and parentId=' . $pid)->select();
        return $rs;
    }

    /**
     * 获取树形分类
     */
    public function getClassAndChild() {
        $m = M('goods_class');
        //获取第一级分类
        $sql = "select * from __PREFIX__goods_class where classFlag=1 and parentId =0 order by classSort asc";
        $rs1 = $m->query($sql);
        if (count($rs1) > 0) {
            $ids = array();
            foreach ($rs1 as $key => $v) {
                $ids[] = $v['classId'];
            }
            //获取第二级分类
            $sql = "select * from __PREFIX__goods_class where classFlag=1 and parentId in (" . implode(',', $ids) . ")  order by classSort asc";
            $rs2 = $m->query($sql);
            if (count($rs2) > 0) {
                $ids = array();
                foreach ($rs2 as $key => $v) {
                    $ids[] = $v['classId'];
                }
                //获取第三级分类
                $sql = "select * from __PREFIX__goods_class where classFlag=1 and parentId in (" . implode(',', $ids) . ")  order by classSort asc";
                $rs3 = $m->query($sql);
                $tmpArr = array();
                if (count($rs3) > 0) {
                    foreach ($rs3 as $key => $v) {
                        $tmpArr[$v['parentId']][] = $v;
                    }
                }
                //把第三级归类到第二级下
                foreach ($rs2 as $key => $v) {
                    $rs2[$key]['child'] = $tmpArr[$v['classId']];
                    $rs2[$key]['childNum'] = count($tmpArr[$v['classId']]);
                }
                $tmpArr = array();
                foreach ($rs2 as $key => $v) {
                    $tmpArr[$v['parentId']][] = $v;
                }
            }
            //把二季归类到第一级下
            foreach ($rs1 as $key => $v) {
                $rs1[$key]['child'] = $tmpArr[$v['classId']];
                $rs1[$key]['childNum'] = count($tmpArr[$v['classId']]);
            }
        }
        return $rs1;
    }

    /**
     * 迭代获取下级
     */
    public function getChild($ids = array(), $pids = array()) {
        $m = M('goods_class');
        $sql = "select classId from __PREFIX__goods_class where classFlag=1 and parentId in(" . implode(',', $pids) . ")";
        $rs = $m->query($sql);
        if (count($rs) > 0) {
            $cids = array();
            foreach ($rs as $key => $v) {
                $cids[] = $v['classId'];
            }
            $ids = array_merge($ids, $cids);
            return $this->getChild($ids, $cids);
        } else {
            return $ids;
        }
    }

    /**
     * 删除
     */
    public function del() {
        $rd = array('status' => -1);
        //获取子集
        $ids = array();
        $ids[] = (int) I('id');
        $ids = $this->getChild($ids, $ids);
        $m = M('goods_class');
        //把相关的商品下架了
        $sql = "update __PREFIX__goods set isSale=0 where goodsClassId1 in(" . implode(',', $ids) . ")";
        $m->query($sql);
        $sql = "update __PREFIX__goods set isSale=0 where goodsClassId2 in(" . implode(',', $ids) . ")";
        $m->query($sql);
        $sql = "update __PREFIX__goods set isSale=0 where goodsClassId3 in(" . implode(',', $ids) . ")";
        $m->query($sql);
        //设置商品分类为删除状态
        $m->classFlag = -1;
        $rs = $m->where(" classId in(" . implode(',', $ids) . ")")->save();
        if (false !== $rs) {
            $rd['status'] = 1;
        }
        return $rd;
    }

    /**
     * 显示分类是否显示/隐藏
     */
    public function editiIsShow() {
        $rd = array('status' => -1);
        if (I('id', 0) == 0)
            return $rd;
        $isShow = (int) I('isShow');
        //获取子集
        $ids = array();
        $ids[] = (int) I('id');
        $ids = $this->getChild($ids, $ids);
        $m = M('goods_class');
        if ($isShow != 1) {
            //把相关的商品下架了
            $sql = "update __PREFIX__goods set isSale=0 where goodsClassId1 in(" . implode(',', $ids) . ")";
            $m->query($sql);
            $sql = "update __PREFIX__goods set isSale=0 where goodsClassId2 in(" . implode(',', $ids) . ")";
            $m->query($sql);
            $sql = "update __PREFIX__goods set isSale=0 where goodsClassId3 in(" . implode(',', $ids) . ")";
            $m->query($sql);
        }
        $m->isShow = ($isShow == 1) ? 1 : 0;
        $rs = $m->where("classId in(" . implode(',', $ids) . ")")->save();
        if (false !== $rs) {
            $rd['status'] = 1;
        }
        return $rd;
    }

}

;
?>